/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file threadedPipe.h
  \brief 

  \author Jack Elston
  $Date: 2006/10/25 13:34:45 $
*/

#ifndef __THREADEDPIPE_H
#define __THREADEDPIPE_H

#include "String.h"
#include "Thread.h"
#include <linux/limits.h>

/*<-------Defines--------->*/
/*! \brief socket buffer size. */
#define FIFO_BUF_SIZE PIPE_BUF   // # bytes in atomic write to a pipe
#define FIFO_HDR_SIZE 12
/*<------End Defines------>*/

/************************************************************\
  |                      ThreadedPipe                          |
  \************************************************************/
/*!
  \class ThreadedPipe
  \brief 
  */
typedef enum {
	RAW = 0,
	STREAM,
	CMD
} FifoType_t;

typedef struct _FifoMsg_t
{
	in_addr    src;
	in_addr    dest;
	uint16_t   port;
	uint16_t   size;
	FifoType_t type;
	uint8_t   *data;

	_FifoMsg_t(uint8_t *buf=NULL) { data=buf; type = RAW; size = port = 0; }

} __attribute__ ((packed)) FifoMsg_t;

class ThreadedPipe : public pThread
{
	protected:
		FifoMsg_t fifo_msg;
		uint8_t   buf[FIFO_BUF_SIZE];

		int    fifo_FDs[4];
		String fifo_names[4];

		// File logging
		FILE * logfile;
		String log_name;
		void   update_gps_str();
		char   gpsStr[256];

	public:

		enum InputType {	
			INBOARD_TX=0,
			INBOARD_RX, 	
			OUTBOARD_TX,
			OUTBOARD_RX
		};

		enum commMode { TX, RX, DUPLEX };

		ThreadedPipe(String name);
		~ThreadedPipe() {};

		bool addFifo(InputType type, String name="", FifoType_t data=RAW);

		String getFifoName(InputType type);

		int readFifo(InputType type);
		int readFifo(int fd);

		int writeFifo(InputType type);
		int writeFifo(int fd);

		void ACK(InputType type);
		void NACK(InputType type);

		void subscribe(uint8_t id, Client * a_client, bool onOff, InputType type);

		virtual commMode mode() {return DUPLEX;};
		virtual FifoType_t type() {return RAW;};

		void update() {};
		void close();
};

#endif //define
